import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";
import pluginReactConfig from "eslint-plugin-react/configs/jsx-runtime.js";
import prettierConfig from "eslint-plugin-prettier/recommended";
import compat from "eslint-plugin-compat";
import html from "@html-eslint/eslint-plugin";
import htmlParser from "@html-eslint/parser";
import node from "eslint-plugin-n";
import eslintPluginUnicorn from "eslint-plugin-unicorn";
import hooksPlugin from "eslint-plugin-react-hooks";

export default [
  /* Prettier */
  prettierConfig,
  /* 页面代码 */
  ...[
    pluginJs.configs.recommended,
    compat.configs["flat/recommended"],
    ...tseslint.configs.strictTypeChecked,
    ...tseslint.configs.stylisticTypeChecked,
    pluginReactConfig,
    eslintPluginUnicorn.configs["flat/recommended"],
    hooksPlugin.configs.recommended,
  ].map((conf) => ({
    ...conf,
    languageOptions: {
      globals: globals.browser,
      parser: tseslint.parser,
      parserOptions: {
        project: true,
        tsconfigRootDir: import.meta.dirname,
        ecmaFeatures: { jsx: true },
      },
    },
    plugins: {
      "@typescript-eslint": tseslint.plugin,
      compat,
      unicorn: eslintPluginUnicorn,
      "react-hooks": hooksPlugin,
    },
    settings: {
      ...conf.settings,
      lintAllEsApis: true,
    },
    rules: {
      ...conf.rules,
      "unicorn/filename-case": "off",
      "unicorn/no-nested-ternary": "off",
      "unicorn/prefer-top-level-await": "off",
      "unicorn/prefer-string-replace-all": "off",
      "@typescript-eslint/no-dynamic-delete": "off",
      "@typescript-eslint/no-misused-promises": [
        "error",
        {
          checksVoidReturn: {
            arguments: false,
            attributes: false,
          },
        },
      ],
    },
    files: ["**/*.ts", "**/*.tsx"],
  })),
  /* Node */
  ...[
    pluginJs.configs.recommended,
    node.configs["flat/recommended-script"],
    eslintPluginUnicorn.configs["flat/recommended"],
  ].map((conf) => ({
    ...conf,
    languageOptions: {
      globals: globals.node,
    },
    plugins: {
      n: node,
      unicorn: eslintPluginUnicorn,
    },
    rules: {
      ...conf.rules,
      "unicorn/filename-case": "off",
      "unicorn/no-nested-ternary": "off",
    },
    files: ["**/*.cjs", "**/*.mjs"],
  })),
  /* HTML */
  {
    ...html.configs["flat/recommended"],
    files: ["**/*.html"],
    rules: {
      ...html.configs["flat/recommended"].rules,
      "@html-eslint/indent": ["error", 2],
      "@html-eslint/use-baseline": "off",
    },
    plugins: { "@html-eslint": html },
    languageOptions: {
      parser: htmlParser,
    },
  },
  /* 排除 */
  {
    ignores: ["dist/*"],
  },
];
